<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @filename morders.php
 * 新闻公告模型
 * @author hmw
 *
 */

class Morders extends Model {

    function Morders() {
        parent::Model();
    }

    function check_orderid_available($orderid){
    	$query = $this->db->get_where('orders',array('order_id'=>$orderid));
    	return $query->row_array();
    }

    /**
     * 将新订单插入数据库
     *
     * @param unknown_type $values
     * @return unknown
     */
    function insert_new_order($values){
    	return $this->db->insert('orders',$values);
    }

    function check_orderid_exist($orderid){
    	$query = $this->db->get_where('orders',array('order_id'=>$orderid,'order_state !='=>-1));
    	$row = $query->row_array();
    	return empty($row) ? FALSE : TRUE;
    }

    function get_order_dai($orderid){
    	$query = $this->db->get_where('orders',array('order_id'=>$orderid));
    	$row = $query->row_array();
    	return empty($row) ? FALSE : $row['dai'];
    }

    function get_order_uid($orderid){
    	$query = $this->db->get_where('orders',array('order_id'=>$orderid));
    	$row = $query->row_array();
    	return empty($row) ? FALSE : $row['uid'];
    }

    /**
     * 获取最新的N条订单
     *
     * @param int $limit
     * @return array/FALSE
     */
    function get_latest_orders($offset=0,$limit=20){
        $this->db->where('order_state !=','-1');
    	$this->db->limit($limit,$offset);
    	$this->db->order_by('order_time','desc');
    	$query = $this->db->get('orders');
    	return $query->result_array();
    }

    function get_order_info($order_id) {
        $query = $this->db->get_where('orders',array('order_id'=>$order_id));
        return $query->row_array();
    }

    function get_search_result($type,$word) {
        $this->db->where('order_state !=','-1');
        $this->db->like($type,$word);
        $query = $this->db->get('orders');
        return $query->result_array();
    }

    function get_search_count2($type,$word){
    	$this->db->where('order_state !=','-1');
        $this->db->like($type,$word);
        return $this->db->count_all_results('orders');
    }

    function get_member_order_count($uid) {
    	$utype = substr(strtoupper($uid),0,2);
        $sql = "SELECT COUNT(*) AS total FROM ".$this->db->dbprefix."orders WHERE ";
        $sql .= " order_state>0";
        if($utype == 'WB'){
        	$sql .= " AND dai_state < 8";
    	} else {
    		$sql .= " AND order_state < 5";
    	}
    	$sql .= " AND (uid='$uid' OR dai='$uid')";
        $query = $this->db->query($sql);
        $row = $query->row();
        return $row->total;
    }

    function get_nb_order_count($nb,$year,$month){
        $sql = "SELECT COUNT(*) AS total FROM ".$this->db->dbprefix."orders WHERE ";
        $sql .= " order_state>0";
    	$sql .= " AND uid='$nb' AND year(order_time)=$year AND month(order_time)=$month";
        $query = $this->db->query($sql);
        $row = $query->row();
        return $row->total;
    }

    function get_nb_orders($nb,$year,$month,$offset=0,$limit=10){
        $sql = "SELECT * FROM ".$this->db->dbprefix."orders
                WHERE order_state>0
                    AND uid='$nb'
                    AND year(order_time)=$year
                    AND month(order_time)=$month
                LIMIT $offset,$limit";
        $query = $this->db->query($sql);
        $rows = $query->result_array();
        return $rows;
    }

 	function get_member_order($uid,$limit=10,$offset=0){
        $utype = substr(strtoupper($uid),0,2);
        $sql = "SELECT * FROM ".$this->db->dbprefix."orders WHERE ";
        $sql .= " order_state>0";
        if($utype == 'WB'){
        	$sql .= " AND dai_state < 8";
    	} else {
    		$sql .= " AND order_state < 5";
    	}
    	$sql .= " AND (uid='$uid' OR dai='$uid')";
        $sql .= " ORDER BY order_time DESC ";
        $sql .= " LIMIT $offset,$limit";
        //echo $sql;exit;
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    function get_history_order_count($uid){
        $sql = "SELECT COUNT(*) AS total FROM ".$this->db->dbprefix."orders WHERE (uid='$uid' OR dai='$uid')";
        $utype = substr(strtoupper($uid),0,2);
    	if($utype == 'WB'){
        	$sql .= " AND dai_state = 8";
    	} else {
    		$sql .= " AND order_state = 5";
    	}
        $query = $this->db->query($sql);
        $row = $query->row();
        return $row->total;
    }

	function get_history_order($uid,$limit=10,$offset=0){
        $sql = "SELECT * FROM ".$this->db->dbprefix."orders WHERE (uid='$uid' OR dai='$uid')";
		$utype = substr(strtoupper($uid),0,2);
    	if($utype == 'WB'){
        	$sql .= " AND dai_state = 8";
    	} else {
    		$sql .= " AND order_state = 5";
    	}
        $sql .= " ORDER BY order_time DESC ";
        $sql .= " LIMIT $offset,$limit";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    function update_order_data($order_id,$data){
    	return $this->db->update('orders',$data,array('order_id'=>$order_id));
    }

    function get_order_count(){
    	//$this->db->where();
        $this->db->where('order_state !=','-1');
    	return $this->db->count_all_results('orders');
    }

    function get_search_count($state='',$word='',$start,$end){
    	$word = $word == '0' ? '' : $word;

    	$sql = "SELECT COUNT(*) AS total FROM (`".$this->db->dbprefix."orders`) WHERE `order_state` != '-1'";

    	if ($state){
    		$sql .= " AND `order_state` = '$state'";
    	}
    	if ($start){
    		$sql .= " AND `order_time` >= '$start'";
    	}
    	if($end){
    		$sql .= " AND `order_time` <= '$end'";
    	}
    	if($word){
    		$sql .= " AND (`order_id` LIKE '%$word%' OR `uid` LIKE '%$word%' OR `tel` LIKE '%$word%' OR `qq` LIKE '%$word%')";
    	}
    	$query = $this->db->query($sql);
    	$row = $query->row();
    	return $row->total;
    	//echo $this->db->last_query();exit;
    }

    function get_search_orders($state,$word='',$offset=0,$limit=10,$start,$end){
    	$word = $word == '0' ? '' : $word;

    	$sql = "SELECT * FROM (`".$this->db->dbprefix."orders`) WHERE `order_state` != '-1'";

    	if ($state){
    		$sql .= " AND `order_state` = '$state'";
    	}
    	if ($start){
    		$sql .= " AND `order_time` >= '$start'";
    	}
    	if($end){
    		$sql .= " AND `order_time` <= '$end'";
    	}
    	if($word){
    		$sql .= " AND (`order_id` LIKE '%$word%' OR `uid` LIKE '%$word%' OR `tel` LIKE '%$word%' OR `qq` LIKE '%$word%')";
    	}
    	$sql .= " ORDER BY `id` DESC";
    	$sql .= " LIMIT $offset,$limit";
    	$query = $this->db->query($sql);
    	//echo $this->db->last_query();
        return $query->result_array();
    }

    function get_orders($offset=0,$limit=10){
        $this->db->where('order_state !=','-1');
    	$this->db->order_by('id','desc');
    	$this->db->limit($limit,$offset);
    	$query = $this->db->get('orders');
    	return $query->result_array();
    }

    /**
     * 订单折扣信息
     * @param $order_id
     * @return unknown_type
     */
    function get_order_discount($order_id){
    	$query = $this->db->get_where('discount',array('order_id'=>$order_id));
    	return $query->row_array();
    }

}

?>